package com.moro.lisp.tools;

import java.util.ArrayList;
import java.util.List;

import com.moro.lisp.JLispData;
import com.moro.lisp.JLispType;
import com.moro.lisp.typedefine.Atom;
import com.moro.lisp.typedefine.Error;

/**
 * 符号池，用来存储和管理终端的所有符号，确保字面值相同的符号就是同一个符号
 * @author HJ
 *
 */
public class SymbolPool {
	private List<Atom> pool;
	
	
	/*
	 * 创建一个供终端使用的符号池，需要预先存储默认的内置符号
	 */
	public SymbolPool() {
		this.pool = new ArrayList<Atom>();
		this.pool.add(new Atom("t"));
		this.pool.add(new Atom("nil"));
		this.pool.add(new Atom("&rest"));//对自身求值的原子，没有值栈和函数值
		
		
	}
	
	public Atom getSymbolByString(String name) {
		Atom atom = findByString(name);
		if(null == atom) {
			atom = new Atom(name);
			this.pool.add(atom);
			return atom;
		}else {
			return atom;
		}
	}
	
	public Atom findByString(String name) {
		
		for(Atom a : this.pool) {
			if(name.equalsIgnoreCase(a.name)) {
				return a;
			}
		}
		return null;
	}
	public int getSymbolsSize() {
		return this.pool.size();
	}
	public void removeById(int id) {
		this.pool.remove(id);
	}
	/*public static void main(String[] args) {
		SymbolPool pool = new SymbolPool();
		Atom a = pool.getSymbolByString("a");
		a.setValue(new JLispData(new Atom("b"),JLispType.ATOM));
		JLispData d = a.eval();
		a.resetValue(new JLispData(new Atom("c"),JLispType.ATOM));
		Atom c = (Atom)(a.eval().data);
		System.out.println(c.name);
		String regex = "[-]?[0-9]*[.]?[0-9]*";
		String str = new String("8.");
		System.out.println(str.matches(regex));
		System.out.println(Double.valueOf(str));
		
		System.out.println((char)32+"nihao");
	}*/
}
